package com.platform.hjt.from.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.hjt.base.controller.BaseController;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.from.controller.param.OrderQZParam;
import com.platform.hjt.from.model.FromQjOrder;
import com.platform.hjt.from.model.MoldeQjFrom;
import com.platform.hjt.from.service.FromQjOrderService;

@Controller
@RequestMapping(value="/quanjin")
public class OrderQZController extends BaseController{
	
	@Resource
	private FromQjOrderService fromQjOrderService;

	@RequestMapping(value="/applySave.do")
	@ResponseBody
	public ReturnBody applySave(@Valid @ModelAttribute("moldeQjFrom") MoldeQjFrom moldeQjFrom, BindingResult result){
		return fromQjOrderService.saveFromQjOrder(moldeQjFrom);
	}
	
	/**
	 * 订单申请
	 * @Description 
	 * @author huangjt
	 */
	@RequestMapping(value="/applySave.html")
	public String apply(Model model, String fromId, String processId, String orderId, String taskId, String taskName, boolean readonly){
		model.addAttribute("processId", processId);
		model.addAttribute("orderId", orderId);
		model.addAttribute("taskId", taskId);
		model.addAttribute("taskName", taskName);

		if(StringUtils.isNotBlank(orderId) && StringUtils.isBlank(taskId)){
		//if(readonly){
			FromQjOrder fromQjOrder = fromQjOrderService.getFromQjOrder("processOrderId", orderId);
			model.addAttribute("fromQjOrder", fromQjOrder);
			model.addAttribute("fromQjOrderDetailList", fromQjOrderService.getfromQjOrderDetailList("fromQjOrder.qjOrderId", fromQjOrder.getQjOrderId()));
			return "page/admin/flowFrom/from/order_view_qz";
		} else {
			return "page/admin/flowFrom/from/order_qz";
		}
	}
	
	/**
	 * 主管审核
	 * @Description 
	 * @author huangjt
	 */
	@RequestMapping(value="/directorAuditing.html")
	public String directorAuditing(Model model, String fromId, String processId, String orderId, String taskId, String taskName, boolean readonly){
		model.addAttribute("processId", processId);
		model.addAttribute("orderId", orderId);
		model.addAttribute("taskId", taskId);
		model.addAttribute("taskName", taskName);
		
		if(StringUtils.isNotBlank(orderId)){
			FromQjOrder fromQjOrder = fromQjOrderService.getFromQjOrder("processOrderId", orderId);
			model.addAttribute("fromQjOrder", fromQjOrder);
			model.addAttribute("fromQjOrderDetailList", fromQjOrderService.getfromQjOrderDetailList("fromQjOrder.qjOrderId", fromQjOrder.getQjOrderId()));
		}
		
		//if(StringUtils.isNotBlank(orderId) && StringUtils.isNotBlank(taskId)){
		if(readonly){
			return "page/admin/flowFrom/from/directorAuditing_view_qz";
		} else{
			return "page/admin/flowFrom/from/directorAuditing_qz";
		}
	}
	
	@RequestMapping(value="/directorAuditing.do")
	@ResponseBody
	public ReturnBody directorAuditingDo(Model model, String processId, String orderId, String taskId){
		return fromQjOrderService.handleDirectorAuditing(processId, orderId, taskId);
	}
	
	/**
	 * 生产部门审核
	 * @Description 
	 * @author huangjt
	 */
	@RequestMapping(value="/scbmsh.html")
	public String scbmsh(Model model, String fromId, String processId, String orderId, String taskId, String taskName, boolean readonly){
		model.addAttribute("processId", processId);
		model.addAttribute("orderId", orderId);
		model.addAttribute("taskId", taskId);
		model.addAttribute("taskName", taskName);
		
		if(StringUtils.isNotBlank(orderId)){
			FromQjOrder fromQjOrder = fromQjOrderService.getFromQjOrder("processOrderId", orderId);
			model.addAttribute("fromQjOrder", fromQjOrder);
			
			
			Map<String, Object>map = new HashMap<String, Object>();
			map.put("fromQjOrder.qjOrderId", fromQjOrder.getQjOrderId());
			map.put("isAllow", true);
			
			model.addAttribute("fromQjOrderDetailList", fromQjOrderService.getfromQjOrderDetailList(map));
		}
		
		return "page/admin/flowFrom/from/scbmsh_qz";
	}
	
	@RequestMapping(value="/scbmsh.do")
	@ResponseBody
	public ReturnBody scpdDo(Model model, @ModelAttribute("orderQZParam")OrderQZParam orderQZParam, String processId, String orderId, String taskId){
		return fromQjOrderService.handleScbmsh(orderQZParam,processId, orderId, taskId);
	}
	
	/**
	 * 生产排单
	 * @Description 
	 * @author huangjt
	 */
	@RequestMapping(value="/scpd.html")
	public String scpdPage(Model model, String fromId, String processId, String orderId, String taskId, String taskName, boolean readonly){
		model.addAttribute("processId", processId);
		model.addAttribute("orderId", orderId);
		model.addAttribute("taskId", taskId);
		model.addAttribute("taskName", taskName);
		
		if(StringUtils.isNotBlank(orderId)){
			FromQjOrder fromQjOrder = fromQjOrderService.getFromQjOrder("processOrderId", orderId);
			model.addAttribute("fromQjOrder", fromQjOrder);
			
			Map<String, Object>map = new HashMap<String, Object>();
			map.put("fromQjOrder.qjOrderId", fromQjOrder.getQjOrderId());
			map.put("isAllow", true);
			//map.put("isProduce", true);
			
			model.addAttribute("fromQjOrderDetailList", fromQjOrderService.getfromQjOrderDetailList(map));
		}
		
		return "page/admin/flowFrom/from/scpd_qz";
	}


	@RequestMapping(value="/scpd.do")
	@ResponseBody
	public ReturnBody scbmshDo(Model model, String processId, String orderId, String taskId){
		return fromQjOrderService.handleScpd(processId, orderId, taskId);
	}
	
	
	/**
	 * 产品关联
	 * @Description 
	 * @author huangjt
	 */
	@RequestMapping(value="/productAssociation.html")
	public String productAssociation(Model model, String processId, String orderId, String taskId, String taskName, boolean readonly){
		
		model.addAttribute("processId", processId);
		model.addAttribute("orderId", orderId);
		model.addAttribute("taskId", taskId);
		model.addAttribute("taskName", taskName);
		
		if(StringUtils.isNotBlank(orderId)){
			FromQjOrder fromQjOrder = fromQjOrderService.getFromQjOrder("processOrderId", orderId);
			model.addAttribute("fromQjOrder", fromQjOrder);
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("fromQjOrder.qjOrderId", fromQjOrder.getQjOrderId());
			map.put("isAllow", true);
			map.put("isProduce", true);
			model.addAttribute("fromQjOrderDetailList", fromQjOrderService.getfromQjOrderDetailList(map));
		}
		
		//if(StringUtils.isNotBlank(orderId) && StringUtils.isNotBlank(taskId)){
		if(readonly){
			return "page/admin/flowFrom/from/productAssociation_view_qz";
		} else{
			return "page/admin/flowFrom/from/productAssociation_qz";
		}
	}
	
	@RequestMapping(value="/productAssociation.do")
	@ResponseBody
	public ReturnBody productAssociationDo(Model model, String fromId, String processId, String orderId, String taskId, String taskName){
		return fromQjOrderService.handleproductAssociation(processId, orderId, taskId);
	}
	
	
	
}
